
CROSS_COMPILE   =
AS              = $(CROSS_COMPILE)as
LD              = $(CROSS_COMPILE)ld
CC              = $(CROSS_COMPILE)gcc
CPP             = $(CC) -E
AR              = $(CROSS_COMPILE)ar
NM              = $(CROSS_COMPILE)nm
STRIP           = $(CROSS_COMPILE)strip
OBJCOPY         = $(CROSS_COMPILE)objcopy
OBJDUMP         = $(CROSS_COMPILE)objdump
export AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP

# Accept the target directory path parameter
param-dir = $(dir)

# Remove the final / of the path
_target-path = $(filter %/, $(param-dir))
__target-path = $(if $(_target-path),$(patsubst %/,%,$(_target-path)),$(param-dir))

# Remove the Spaces before and after
target-path = $(strip $(__target-path))

# Parsed file name
target-name = $(notdir $(target-path))

CFLAGS          := -Wall -O2 -g
LDFLAGS         :=
export CFLAGS LDFLAGS

srctree         := $(shell pwd)
export srctree
include $(srctree)/scripts/Makefile.include

TARGET := app

obj-y += main.o
obj-y += drivers/
obj-y += modules/

$(TARGET): built-in.o
	$(Q)$(CC) $^ $(LDFLAGS) -o $(TARGET)

built-in.o: 
	$(Q)$(MAKE) $(mbuild)=.

lib: $(target-path)/built-in.o
	$(AR) cr $(target-path)/$(target-name).a $^
	$(CC) -o $(target-path)/$(target-name).so -shared -fPIC $^

lib_clean:
	$(Q)$(MAKE) $(mclean)=$(target-path)
	rm -rf $(target-path)/$(target-name).a
	rm -rf $(target-path)/$(target-name).so

$(target-path)/built-in.o:
	$(Q)$(MAKE) $(mbuild)=$(target-path)

clean:
	$(Q)$(MAKE) $(mclean)=.
	$(Q)rm -f $(TARGET)

.PHONY:all clean lib_clean
